package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Shader;
import android.text.TextUtils;


import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.MarketRankCoordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;

import java.util.List;

import static com.blockmeta.bbs.businesslibrary.widget.chartview.ChartData.ChartPeroid.CP_1month;


class MarketRankLayer extends BasicLayer {
    protected final double m_dbNone = Double.MIN_VALUE;
    private int beginIdx;//用于存储第一个指标第一个值的位置
    private int m_color1 = 0xFF1890FF, m_color2 = 0xFF1890FF, m_color3 = 0xFFFFFFFF, m_color4 = 0xFF000000;

    public MarketRankLayer() {
        super();
    }

    @Override
    public Coordinate createCoordinate() {
        return new MarketRankCoordinate();
    }

    @Override
    public boolean onCalculateData() {
        return true;
    }


    protected int getDataStartIndex() {
        return getMoveStartIndex() > beginIdx ? getMoveStartIndex() : beginIdx;
    }

    /**
     * 获取数据的移动位置
     *
     * @return
     */
    protected int getMoveStartIndex() {
        Coordinate tCoordinate = m_callback.getCoordinate();
        if (tCoordinate != null) {
            return tCoordinate.getStartIndex();
        }
        return 0;
    }

    @Override
    public Layer.ChartTypeEnum getLayerTypeEnum() {
        return Layer.ChartTypeEnum.MarketRank;
    }

    public List<CandleData> convertDate(List<CandleData> pList) {
        return pList;
    }

    @Override
    public void drawYesterClose(Canvas pCanvas, Paint pPaint) {
    }

    @Override
    public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint) {
        //获取坐标系
        Coordinate tCoordinate = m_callback.getCoordinate();
        //转化处理数据
        List<CandleData> tDataList = convertDate(m_callback.getCandleDataList());

        //定义初始化文本
        String tCloseText = null;
        String tOpenText = null;
        String tHighText = null;
        String tLowText = null;
        String tTimeText = null;
        String tValueText = null;
        String tVolumnText = null;
        int tXPoint = pPoint.x;


        if (tDataList != null && tDataList.size() > 0) {
            int tListSize = tDataList.size();
//			int tStartIndex = tCoordinate.getStartIndex();

            float tXFirstPosition = tCoordinate.getXFirstPosition();
            float tXInterval = tCoordinate.getXInterval();
            float tYFirstposition = tCoordinate.getYFirstPosition();
            float tYMaxValue = tCoordinate.getMaxValue();
            float tYMinValue = tCoordinate.getMinValue();
            float tYValueDist = tYMaxValue - tYMinValue;
            float tYValueLength = tCoordinate.getValueHeight();

            float tCurrentYPosition = 0.f;
            float tTempYPosition = 0.f;
            float tCurrentXPosition = 0.f;
            float tTempXPosition = 0.f;

            int m_bDigit = m_callback.getDigit();

            float tLineWidth = 4.f;
            pPaint.setAntiAlias(true);

            boolean tStartDraw = false;

            int tDotColor = BaseApp.getApp().getResources().getColor(R.color.sys_even);
            int tCulOffset = 1;

            int dataStart = getDataStartIndex(); //tStartIndex + getStartIndex();
            int moveStart = getMoveStartIndex();
            int displayCount = m_callback.getDisplayCount();//获取当前要显示的个数
            int minCount = Math.min(moveStart + displayCount, tListSize);//不能超出数据集宽度
            if (dataStart > tListSize)//如果数据量不足以算出一个值
            {
                return;
            }
            CandleData candle = null;
            Path tAreaPath = new Path();
            RectF tRectF = tCoordinate.getLayerRectF();
            pPaint.setColor(m_callback.getColor());
            pPaint.setStrokeWidth(tLineWidth);
            Paint tPaint = new Paint();
            for (int i = dataStart; i < minCount; i++) {
                //如果收盘价为空就跳过
                candle = tDataList.get(i);


                int rank = (int) candle.m_lVolume;

                tTempXPosition = tCurrentXPosition;
                tCurrentXPosition = tXFirstPosition + tXInterval * (i - moveStart);
                tTempYPosition = tCurrentYPosition;
                tCurrentYPosition = tYFirstposition - ((tYMaxValue - rank) * tYValueLength / tYValueDist);

                if (i == dataStart) {
                    tAreaPath.moveTo(tCurrentXPosition, tCoordinate.getLayerRectF().bottom);
                }

                tAreaPath.lineTo(tCurrentXPosition, tCurrentYPosition);
                if (i == minCount - 1) {
                    tAreaPath.lineTo(tCurrentXPosition, tCoordinate.getLayerRectF().bottom);
                    tAreaPath.lineTo(tXFirstPosition, tCoordinate.getLayerRectF().bottom);
                    Shader mShader = new LinearGradient(0, tRectF.top, 0, tRectF.bottom,
                            Color.argb((Color.alpha(m_color1)) / 3, Color.red(m_color1), Color.green(m_color1), Color.blue(m_color1)), Color.argb((Color.alpha(m_color1)) / 15, Color.red(m_color1), Color.green(m_color1), Color.blue(m_color1)), Shader.TileMode.CLAMP);
                    tPaint.setShader(mShader);
                    tPaint.setStyle(Paint.Style.FILL);
                    pCanvas.drawPath(tAreaPath, tPaint);
                }


                if (tStartDraw) {

                    pPaint.setColor(m_color2);
                    drawLine(pCanvas, pPaint, tCurrentYPosition, tTempYPosition, tCurrentXPosition, tTempXPosition, tCulOffset, tRectF);

                } else {
                    tStartDraw = true;
                }


            }
            tStartDraw = false;
            tLineWidth = 2.f;
            pPaint.setStrokeWidth(tLineWidth);
            pPaint.setStyle(Paint.Style.FILL);
            for (int i = dataStart; i < minCount; i++) {
                //如果收盘价为空就跳过
                candle = tDataList.get(i);

                int rank = (int) candle.m_lVolume;

                tTempXPosition = tCurrentXPosition;
                tCurrentXPosition = tXFirstPosition + tXInterval * (i - moveStart);
                tTempYPosition = tCurrentYPosition;
                tCurrentYPosition = tYFirstposition - ((tYMaxValue - rank) * tYValueLength / tYValueDist);


                if (isChooseLayer(pPoint, tListSize, tXInterval, tCurrentXPosition, dataStart, i)) {
                    tValueText = "No." + String.valueOf(rank);

                    pPaint.setColor(m_color4);
                    pPaint.setStrokeWidth(1.f);
                    pCanvas.drawLine(tCurrentXPosition, tRectF.top, tCurrentXPosition, tRectF.bottom, pPaint);
                    pPaint.setStrokeWidth(tLineWidth);
                    tTimeText = getDisplayX(CP_1month, tDataList.get(i));
                    tXPoint = (int) tCurrentXPosition;
                    pPaint.setColor(m_color2);
                    pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 8, pPaint);
                    pPaint.setColor(m_color3);
                    pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 6, pPaint);

                    float textSize = DimentionsUtil.dp2px(10);
                    pPaint.setTextSize(textSize);
                    pPaint.setColor(m_color2);
                    float tTxtWidth0 = pPaint.measureText(tValueText);
                    if ((tCurrentYPosition - textSize * 2) < tRectF.top) {
                        pCanvas.drawText(tValueText, tCurrentXPosition - tTxtWidth0 / 2, tCurrentYPosition + textSize * 2, pPaint);
                    } else {
                        pCanvas.drawText(tValueText, tCurrentXPosition - tTxtWidth0 / 2, tCurrentYPosition - textSize, pPaint);
                    }


                } else {
                    pPaint.setStrokeWidth(tLineWidth);
                    pPaint.setColor(m_color3);
                    pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 8, pPaint);
                    pPaint.setColor(m_color2);
                    pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 4, pPaint);
                }
            }
        }

        setCrossData(pCanvas, pPaint, tTimeText, tValueText, new Point(tXPoint, pPoint.y));

    }

    private boolean isChooseLayer(Point pPoint, int tListSize, float tXInterval, float tCurrentXPosition, int dataStart, int i) {
        return ((pPoint.x > 0 && pPoint.y > 0) && (((tCurrentXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tCurrentXPosition + tXInterval / 2))
                || ((i == tListSize - 1) && pPoint.x > tCurrentXPosition + tXInterval / 2)
                || (i == dataStart && pPoint.x < tCurrentXPosition - tXInterval / 2)));
    }

    private void drawLine(Canvas pCanvas, Paint pPaint, float tCurrentYPosition, float tTempYPosition, float tCurrentXPosition, float tTempXPosition, int tCulOffset, RectF tRectF) {
        pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
    }

    private String getDigitString(String value, int digit) {
        if (!TextUtils.isEmpty(value)) {
            try {
                value = FormatUtil.getFormatString(Double.parseDouble(value), digit);
            } catch (Exception e) {
            }
        } else {
            value = "";
        }

        return value;
    }

    private float getScale(float start, float end, float middle) {
        return (start - middle) / (start - end);
    }

    private void resetIndicatorData() {
    }

    /**
     * draw layer left－top text
     * 获取指标值进行显示
     *
     * @param pCanvas
     * @param pPaint
     */
    @Override
    public void drawFloatIndicatorText(boolean longtouch, Canvas pCanvas, Paint pPaint) {
    }
}
